package com.house.health.controller;

import com.house.health.domain.*;
import com.house.health.service.IHouserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author DuanChangSuo
 * @date 2023/2/7 21:44
 * @description 类的描述和介绍
 **/
@Controller
public class HouseController {

    @Autowired
    private IHouserService iHouserService;

    /**
     * 首页点击房屋详情，若没有登录，表头显示登录注册，若已经登录，显示个人中心
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/toDetailsPage")
    public String toDetailsPage(int id,HttpServletRequest request) {
        House HouseDetails = iHouserService.findHouseDetailsById(id);
        List<String> list = new ArrayList<String>();
        String[] split = HouseDetails.getHouseDetailsImg().split("~");
        for(int i=0;i<split.length;i++) {
            list.add(split[i]);
        }
        request.getSession().setAttribute("Details", HouseDetails);
        request.getSession().setAttribute("DetailsImg", list);
        return "housedetails";
    }

    /**
     * 管理员或者用户更新房源信息
     * @param house
     * @return
     */
    @RequestMapping("/updateHouse")
    @ResponseBody
    public String updateHouse(House house) {
        int n = iHouserService.updateHouse(house);
        if(n>0)
            return "OK";
        return "FAIL";
    }

    /**
     * 管理员或者用户删除房源信息
     * @param hID
     * @return
     */
    @RequestMapping("/deleteUserHouse")
    @ResponseBody
    public String deleteUserHouse(String hID) {
        int n = iHouserService.deleteUserHouse(Integer.parseInt(hID));
        if(n>0) {
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 在首页的用户登录成功，走这个请求，再回到到首页
     * @param request
     * @return
     */
    @RequestMapping("/toIndexPage")
    public String toIndexPage(HttpServletRequest request) {
        List<House> findHomeInfo = iHouserService.findHomeInfo();
        request.getSession().setAttribute("House", findHomeInfo);
        return "index";
    }

    /**
     * 搜索框搜索
     * @param request
     * @param keywords
     * @return
     */
    @RequestMapping("/findHouseByLike")
    public String findHouseByLike(HttpServletRequest request,String keywords) {
        List<House> findHomeInfo = iHouserService.findHouseByLike(keywords);
        request.getSession().removeAttribute("House");
        request.getSession().setAttribute("House", findHomeInfo);
        return "index";
    }

    /**
     * 升序
     * @param request
     * @return
     */
    @RequestMapping("/findHousrOrderByAsc")
    public String findHousrOrderByAsc(HttpServletRequest request) {
        List<House> findHomeInfo = iHouserService.findHouseOrderByAsc();
        request.getSession().removeAttribute("House");
        request.getSession().setAttribute("House", findHomeInfo);
        return "index";
    }

    /**
     * 降序
     * @param request
     * @return
     */
    @RequestMapping("/findHousrOrderByDesc")
    public String findHousrOrderByDesc(HttpServletRequest request) {
        List<House> findHomeInfo = iHouserService.findHouseOrderByDesc();
        request.getSession().removeAttribute("House");
        request.getSession().setAttribute("House", findHomeInfo);
        return "index";
    }
}